Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resampled wcs #449

Merged
merged 19 commits into from
Aug 6, 2021
Merged

Resampled wcs #449

merged 19 commits into from
Aug 6, 2021

Conversation

DanRyanIrish
Copy link
Member

Description

This PR updates ResampledWCS to account for the fact that pixel indices represent the centres of pixels and therefore a shift must be applied when converting between pixel grids.

Fixes #

@pep8speaks
Copy link

pep8speaks commented Jul 29, 2021

Hello @DanRyanIrish! Thanks for updating this PR.

Line 1949:5: E129 visually indented line with same indent as next logical line
Line 1948:43: W504 line break after binary operator
Line 1947:40: W504 line break after binary operator
Line 1814:13: E129 visually indented line with same indent as next logical line
Line 1813:33: W504 line break after binary operator
Line 1317:52: W504 line break after binary operator
Line 1303:70: W504 line break after binary operator
Line 1299:44: W504 line break after binary operator
Line 877:13: E129 visually indented line with same indent as next logical line
Line 876:38: W504 line break after binary operator
Line 129:13: E129 visually indented line with same indent as next logical line
Line 128:41: W504 line break after binary operator

Comment last updated at 2021-08-06 09:40:05 UTC

@DanRyanIrish DanRyanIrish added this to the 2.1 milestone Jul 29, 2021
@DanRyanIrish
Copy link
Member Author

@Cadair, here are my changes to ResampledWCS. Tests still need fixing. But before I invest that effort, any comments on what I'm doing?

Copy link
Member

@Cadair Cadair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't sat down and worked through the maths of the translation (do you want me to?), but looks good otherwise.

ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
DanRyanIrish and others added 3 commits July 29, 2021 13:20
Co-authored-by: Stuart Mumford <stuart@cadair.com>
Also reverted implementation of ResampledWCS.pixel_bounds.
@DanRyanIrish
Copy link
Member Author

I haven't sat down and worked through the maths of the translation (do you want me to?), but looks good otherwise.

A check on the maths would be welcome but not expected.

@DanRyanIrish
Copy link
Member Author

In going through the tests, I've come across an important point regarding pixel_bounds. My understanding is that pixel_bounds refer to the center of the pixels at either end of the limit of the axis. Currently, ResampledWCS just translates the centre of the underlying pixel_bounds to the new pixel grid. However, this does not represent that limit of the new pixel grid for which the transformations are defined.

Consider a 1-D pixel grid that we upsample by a factor of 1/3, i.e. there are 3 new pixels for every old/underlying pixel. Say the pixel bounds were (-1, 2). The location of the centre of these pixels in the new pixel grid is (-3, 6). However, the full extent of the defined range includes the edges of these pixels. Defined with pixel edges, the pixel_bounds (-1.5, 2.5). In the new pixel grid, this corresponds to (-4.5, 7.5). This means that in the new pixel grid, pixels -4 -- 7 (inclusive) are fully within the pixel bounds of the original pixel grid. However, pixel_bounds currently returns (-3, 6), implying that the transformations aren't defined for new pixels -4 or 7.

In the above example I chose numbers that worked out nicely in whole pixels. But this might not always be the case. We must therefore clarify what pixel_bounds represents. Is it:

  • the centre of the last whole pixel included in the valid range? (If this is the case then resampling can cause the valid range in world coords to shrink a little if a non-integer number of new pixels fit into the pixel bounds.)
  • the centre the the last pixel that has at least some of its area in the valid range? (If this is the case then resampling can cause the valid range in world coords to increase a little for the same reason as above.)
  • the non-integer pixel value representing the edge of the valid range. If this is the case, the pixel bounds can always be kept the same in world coords between the new and old pixel grids.

@DanRyanIrish DanRyanIrish mentioned this pull request Jul 29, 2021
18 tasks
@DanRyanIrish
Copy link
Member Author

According to @ayshih the answer to the above is option 3.

Copy link
Member

@Cadair Cadair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, and I think I understand the new algorithm. I think this at least needs new tests for offset=? also does it not need tests which demonstrate that the new algorithm is required?

ndcube/wcs/wrappers/resampled_wcs.py Show resolved Hide resolved
@DanRyanIrish
Copy link
Member Author

Any further comments before merging this @Cadair? (The failing test will be resolved once #451 is merged.)

Copy link
Contributor

@dhomeier dhomeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some mainly technical remarks; if my comments on the lengths of factor and offset are correct, those checks could perhaps use a test as well.

ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
Co-authored-by: Derek Homeier <dhomeie@gwdg.de>
ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
ndcube/wcs/wrappers/resampled_wcs.py Outdated Show resolved Hide resolved
@DanRyanIrish
Copy link
Member Author

Hi @dhomeier. Thanks so much for your comments. I've taken them into account and will add an extra test as you suggest.

return self._wcs.pixel_shape
underlying_shape = np.asarray(self._wcs.pixel_shape)
int_elements = np.isclose(np.mod(underlying_shape, self._factor), 0,
atol=np.finfo(float).resolution)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually

        int_elements = np.isclose(underlying_shape / self._factor, np.rint(underlying_shape / self._factor),
                                  atol=np.finfo(float).resolution)

might be the most robust way to check this.

@Cadair Cadair merged commit a0076b4 into sunpy:main Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants